<template>
  <div style="padding: 15px;">
    <div style="margin-bottom: 15px;overflow: hidden">
      <Input style="width: 200px;float: left;" placeholder="请输入机构名称"/>
      <Input style="width: 200px;float: left;" placeholder="请输入机构联系人"/>
      <al-selector v-model="cityList" level= 0 placeholder style="width: 200px;float: left;" @on-change="cityChange"/>
      <Button @click="searchOrg" style="margin-left: 10px;float: left;" type="success">搜索</Button>
      <Button @click="add" type="primary" style="float: right;">添加机构</Button>
    </div>
    <Table border ref="selection" :columns="orgTitle" :data="orgList"></Table>
    <Page :total="total" show-sizer style="margin-top: 15px;"  @on-change="pageChange" @on-page-size-change="pageSizeChange"/>
    <Modal v-model="orgModal" :title="modalTitle" @on-ok="submit" @on-cancel="cancel">
      <Form :model="orgForm" :label-width="80">
        <FormItem label="机构名称">
          <Input v-model="orgForm.name" placeholder="请输入机构名称"/>
        </FormItem>
        <FormItem label="所在省市">
          <al-selector v-if="showCity" v-model="orgForm.city" level= 1 placeholder style="padding-left: 6px;" @on-change="cityListChange"/>
        </FormItem>
        <FormItem label="联系地址">
          <Input v-model="orgForm.address" placeholder="请输入联系地址"/>
        </FormItem>
        <FormItem label="机构描述">
          <Input v-model="orgForm.remark" type="textarea" :autosize="{minRows: 2,maxRows: 3}" placeholder="请输入机构描述"/>
        </FormItem>
        <FormItem label="联系人">
          <Input v-model="orgForm.contact" placeholder="请输入联系人"/>
        </FormItem>
        <FormItem label="联系电话">
          <Input v-model="orgForm.telephone" placeholder="请输入联系电话"/>
        </FormItem>
        <FormItem label="机构logo">
          <div class="demo-upload-list" v-for="item in logoUploadList">
            <template v-if="item.status === 'finished'">
              <img :src="item.url">
              <div class="demo-upload-list-cover">
                <Icon type="ios-eye-outline" @click.native="handleView(item.url)"></Icon>
                <Icon type="ios-trash-outline" @click.native="logoRemove(item)"></Icon>
              </div>
            </template>
            <template v-else>
              <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
            </template>
          </div>
          <Upload
            ref="uploadq"
            :show-upload-list="false"
            :on-success="logoSuccess"
            :format="['jpg','jpeg','png']"
            :before-upload="logoHandleBeforeUpload"
            multiple
            type="drag"
            accept="image/*"
            action="//47.110.52.83:8080/api/picture/upload"
            style="display: inline-block;width:58px;">
            <div style="width: 58px;height:58px;line-height: 58px;">
              <Icon type="ios-camera" size="20"></Icon>
            </div>
          </Upload>
        </FormItem>
        <FormItem label="机构背景图">
          <div class="demo-upload-list" v-for="item in bgUploadList">
            <template v-if="item.status === 'finished'">
              <img :src="item.url">
              <div class="demo-upload-list-cover">
                <Icon type="ios-eye-outline" @click.native="handleView(item.url)"></Icon>
                <Icon type="ios-trash-outline" @click.native="bgRemove(item)"></Icon>
              </div>
            </template>
            <template v-else>
              <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
            </template>
          </div>
          <Upload
            ref="upload"
            :show-upload-list="false"
            :on-success="bgSuccess"
            :format="['jpg','jpeg','png']"
            :before-upload="bgHandleBeforeUpload"
            multiple
            type="drag"
            accept="image/*"
            action="//47.110.52.83:8080/api/picture/upload"
            style="display: inline-block;width:58px;">
            <div style="width: 58px;height:58px;line-height: 58px;">
              <Icon type="ios-camera" size="20"></Icon>
            </div>
          </Upload>
          <Modal title="查看大图" v-model="visible">
            <img :src="imgName" v-if="visible" style="width: 100%">
          </Modal>
        </FormItem>
        <FormItem label="状态">
          <i-switch size="large" v-model="orgForm.state">
            <span slot="open">启用</span>
            <span slot="close">禁用</span>
          </i-switch>
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>

<script>
  import {formatDate} from '_com/date'
  export default {
    name: 'index',
    data () {
      return {
        searchLinkName: '',
        searchName: '',
        searchProvinceCode: '',
        number: 0,
        size: 10,
        total: 0,
        showCity: false,
        orgForm: {
          contact: '',
          address: '',
          name: '',
          telephone: '',
          state: true,
          cityCode: '',
          provinceCode: '',
          remark: '',
          city: []
        },
        orgAdmin: '',
        cityList: [],
        orgTitle: [
          {
            title: '机构名称',
            key: 'name',
            align: 'center',
            width: 160
          },
          {
            title: '所在省市',
            align: 'center',
            render: (h, params) => {
              return h('div', params.row.provinceName + '，' + params.row.cityName)
            }
          },
          {
            title: '联系地址',
            key: 'address',
            align: 'center',
            width: 200
          },
          {
            title: '联系人',
            key: 'contact',
            align: 'center'
          },
          {
            title: '联系电话',
            key: 'telephone',
            align: 'center'
          },
          {
            title: '创建时间',
            key: 'createTime',
            align: 'center',
            width: 150,
            render: (h, params) => {
              return h('div', formatDate(new Date(params.row.createTime), 'yyyy-MM-dd hh:mm'))
            }
          },
          {
            title: '添加人',
            key: 'createName',
            align: 'center'
          },
          {
            title: '状态',
            key: 'status',
            width: 120,
            align: 'center',
            render: (h, params) => {
              const row = params.row
              const color = row.state === '1' ? 'primary' : row.state === '0' ? 'error' : ''
              const text = row.state === '1' ? '正常' : row.state === '0' ? '禁用' : ''
              return h('Tag', {
                props: {
                  type: 'dot',
                  color: color
                }
              }, text)
            }
          },
          {
            title: '操作',
            key: 'action',
            width: 150,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.editor()
                      let row = params.row
                      let arr = [row.provinceCode, row.cityCode]
                      this.orgForm.contact = row.contact
                      this.orgForm.address = row.address
                      this.orgForm.name = row.name
                      this.orgForm.telephone = row.telephone
                      this.orgForm.cityCode = row.cityCode
                      this.orgForm.provinceCode = row.provinceCode
                      this.orgForm.city = arr
                      this.orgForm.remark = row.remark
                      this.logoPic = row.orgLogo
                      this.logoUploadList = row.orgLogo === null ? [] : [{url: row.orgLogo, status: 'finished'}]
                      this.bgPic = row.bgImage
                      this.bgUploadList = row.bgImage === null ? [] : [{url: row.bgImage, status: 'finished'}]
                      this.orgForm.id = row.id
                      if (row.state === '1') {
                        this.orgForm.state = true
                      } else {
                        this.orgForm.state = false
                      }
                    }
                  }
                }, '编辑'),
                h('Button', {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '删除',
                        content: '你确定要删除吗？',
                        onOk: () => {
                          this.remove(params.row.id)
                        }
                      })
                    }
                  }
                }, '删除')
              ])
            }
          }
        ],
        orgList: [],
        areaModel: '',
        orgModal: false,
        modalTitle: '',
        logoPic: '',
        logoUploadList: [],
        bgPic: '',
        bgUploadList: [],
        imgName: '',
        visible: false
      }
    },
    mounted() {
      this.orgPage()
    },
    methods: {
      orgPage() {
        let url = '/api/api/sys/org/page'
        let form = {
          linkName: this.searchLinkName,
          name: this.searchName,
          provinceCode: this.searchProvinceCode,
          number: this.number,
          size: this.size
        }
        this.$get(url, form).then((res) => {
          if (res.code === 0) {
            this.orgList = res.data
            this.total = res.total
          }
        })
      },
      title(title) {
        this.orgModal = true
        this.modalTitle = title
        this.showCity = true
      },
      add() {
        this.title('添加机构')
      },
      editor() {
        this.title('编辑机构')
      },
      submit() {
        if (this.modalTitle === '添加机构') {
          this.addOrg()
        } else {
          this.theEditor()
        }
      },
      addOrg() {
        let url = '/api/api/sys/org/create'
        let form = {
          bgImage: this.bgPic,
          orgLogo: this.logoPic,
          contact: this.orgForm.contact,
          address: this.orgForm.address,
          name: this.orgForm.name,
          remark: this.orgForm.remark,
          telephone: this.orgForm.telephone,
          state: Number(this.orgForm.state),
          cityCode: this.orgForm.cityCode,
          provinceCode: this.orgForm.provinceCode
        }
        this.$post(url, form).then((res) => {
          if (res.code === 0) {
            this.orgPage()
            this.cancel()
            this.$Modal.info({
              title: '添加成功',
              content: '默认密码为：123456'
            })
          } else {
            this.$Message.error('添加失败')
          }
        })
      },
      theEditor() {
        let url = '/api/api/sys/org/update'
        let form = {
          bgImage: this.bgPic,
          orgLogo: this.logoPic,
          contact: this.orgForm.contact,
          address: this.orgForm.address,
          name: this.orgForm.name,
          remark: this.orgForm.remark,
          telephone: this.orgForm.telephone,
          state: Number(this.orgForm.state),
          cityCode: this.orgForm.cityCode,
          provinceCode: this.orgForm.provinceCode,
          id: this.orgForm.id
        }
        this.$put(url, form).then((res) => {
          if (res.code === 0) {
            this.$Message.success('修改成功')
            this.orgPage()
            this.cancel()
          } else {
            this.$Message.error('修改失败')
          }
        })
      },
      remove(id) {
        let url = '/api/api/sys/org/delete'
        this.$deletes(url, {id: id}).then((res) => {
          if (res.code === 0) {
            this.$Message.success('删除成功')
            this.orgPage()
          } else {
            this.$Message.error('删除失败')
          }
        })
      },
      searchOrg () {
        this.orgPage()
      },
      cancel() {
        this.orgForm = {}
        this.logoPic = ''
        this.logoUploadList = ''
        this.bgPic = ''
        this.bgUploadList = ''
        this.orgForm.state = true
        this.showCity = false
      },
      cityListChange(change) {
        this.orgForm.provinceCode = change[0].code
        if (change.length >= 2) {
          this.orgForm.cityCode = change[1].code
        }
      },
      cityChange(change) {
        this.searchProvinceCode = change[0].code
        this.orgPage()
      },
      pageChange(page) {
        this.number = page - 1
        this.orgPage()
      },
      pageSizeChange(page) {
        this.size = page
        this.orgPage()
      },
      handleView (url) {
        this.imgName = url
        this.visible = true
      },
      logoRemove (file) {
        let url = '/api/api/picture/delete'
        let fileUrl = {
          fileUrl: file.url
        }
        this.$deletes(url, fileUrl).then((res) => {
          if (res.message) {
            this.coverPic.splice(this.pic.indexOf(file), 1)
            this.coverUploadList.splice(this.coverUploadList.indexOf(file), 1)
            this.$Message.success('删除成功')
          } else {
            this.$Message.error('删除失败')
          }
        })
      },
      bgRemove (file) {
        let url = '/api/api/picture/delete'
        let fileUrl = {
          fileUrl: file.url
        }
        this.$deletes(url, fileUrl).then((res) => {
          if (res.message) {
            this.bgPic.splice(this.pic.indexOf(file), 1)
            this.bgUploadList.splice(this.bgUploadList.indexOf(file), 1)
            this.$Message.success('删除成功')
          } else {
            this.$Message.error('删除失败')
          }
        })
      },
      logoSuccess(res, file) {
        file.url = file.response.data
        this.$Message.success('上传成功')
        this.logoPic = file.response.data
        this.logoUploadList.push(file)
      },
      bgSuccess(res, file) {
        file.url = file.response.data
        this.$Message.success('上传成功')
        this.bgPic = file.response.data
        this.bgUploadList.push(file)
      },
      bgHandleBeforeUpload () {
        const check = this.bgUploadList.length < 1
        if (!check) {
          this.$Message.error('最多上传1张图片')
        }
        return check
      },
      logoHandleBeforeUpload() {
        const check = this.logoUploadList.length < 1
        if (!check) {
          this.$Message.error('最多上传1张图片')
        }
        return check
      }
    }
  }
</script>

<style scoped>
  .demo-upload-list{
    display: inline-block;
    width: 60px;
    height: 60px;
    text-align: center;
    line-height: 60px;
    border: 1px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    box-shadow: 0 1px 1px rgba(0,0,0,.2);
    margin-right: 4px;
  }
  .demo-upload-list img{
    width: 100%;
    height: 100%;
  }
  .demo-upload-list-cover{
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,.6);
  }
  .demo-upload-list:hover .demo-upload-list-cover{
    display: block;
  }
  .demo-upload-list-cover i{
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
  }
</style>
